function trimString (str)
{
 while (str.substring(0,1) == ' ')
 {
  str= str.substring(1, str.length);
 }
 while (str.substring(str.length-1, str.length) == ' ')
 {
  str = str.substring(0,str.length-1);
 }
 return str;
}

function valid_email(theValue)
{
	if (theValue.length < 4)
    {
    	alert("Email is invalid.");
    	return false;
    }
    else if ((theValue.indexOf("@") != -1) && 
          (theValue.indexOf("@") < theValue.lastIndexOf(".")))
    {
    	return true;
    }
    else 
    {
    	alert("Use a proper email");
    }    
    return false;
}

function submitForm(id) {
	var form = document.getElementById(id);
//	for (var j=0; j < form.elements.length; j++) {
//		var elem = form.elements[j];
//		alert(elem.name + ' is ' + elem.value);
//	}
	form.submit();
}

function submitLogin() {
	
	var form = document.login;
	form.email.value = trimString(form.email.value);
	if (! valid_email(form.email.value)) {
		return;
	}
	
    form.pw.value = trimString(form.pw.value);
    if (form.pw.value.length < 4) {
		alert("Enter your password to login.");
		return;
	}	

	form.submit();
}

function submitLoginByKey() {
	
	var form = document.loginbykey;
	form.otp.value = trimString(form.otp.value);
    if (form.otp.value.length < 10) {
		alert("Click on your YubiKey button to log in");
		return;
	} else {
		form.submit();
	}
}

function clearInput(id)
{
	document.getElementById(id).value='';
}

function popup (url, h)
{
  //Create a new window
  var newWin = window.open(url, "", 
   'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=' + h);
  newWin.focus();
}

function confirmRemove(formName) {
	var form = document.getElementById(formName);
	if (!confirm("Do you really want to remove it?")) {
		form.remove.value = 0;
		//form.submit();
		//window.location=history.go(-1);
	} else {
		form.submit();		
	}
}

function focusInput() {
    document.forms[0][0].focus();
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

var addKeyShown = false;
var disablePWShown = false;

function submitKeyMgmtForm(act) {
	var form = document.keymgmt;
	form.act.value = act;
	form.submit();
}

function showAddKey() 
{
    if (addKeyShown) {
		document.getElementById('addkey').innerHTML = '';
    	addKeyShown = false;
    } else {        
        document.getElementById('addkey').innerHTML = '<form method=post action=yubi_mgmt.php id=addyubikey name=addyubikey>\
<input name=act value=newkey type=hidden>\
<table border=0 width=100%>\
<tr><td><b>&nbsp;&nbsp; First, make sure the admin Yubikey you add here is owned by you already.</b></td></tr>\
<tr><td>&nbsp;&nbsp;&nbsp;Use a password to protect the account access:</td>\
<td align=left><input name=pin id=pin size=20 maxlength=80 type=password>\
</td></tr>\
<tr><td>&nbsp;&nbsp;&nbsp;Add a note about this Yubikey:</td>\
<td align=left><input name=note id=note size=45 maxlength=45>\
</td></tr>\
<tr><td>&nbsp;&nbsp;&nbsp;Touch the button on your Yubikey:</td>\
<td align=left><input name=otp id=otp size=45 maxlength=100>\
&nbsp; <input type=button class=buttonLinkO value=Add \
onClick="submitForm(\'addyubikey\');">\
</td></tr></table></form>';
        addKeyShown = true;
    }
}

function showImpKey(impKeys, client) 
{
//	alert('addKeyShown = ' + addKeyShown);
    if (impKeys == 1) {
		document.getElementById('impkey').innerHTML = '';
        document.getElementById('impkeys').innerHTML = '<form method=post action=import_keys.php id=add_keys name=add_keys enctype="multipart/form-data">\
<input name=act value=impkeys type=hidden>\
<table border=0 width=100%>\
<tr><td colspan=2>\
<h4>Upload a tab-separated text file with following fields:</h4><br>\
Client-ID, Yubikey tokenID (in base64), AES Secret (in base64), \
Life Cycle State, Notes about this Yubikey\
</td></tr>\
<tr><td width=50%>\
<input name=fname type=file size=40 class=button>\
</td>\
<td align=left>\
<input type=submit class=buttonLinkO value="Upload"\
onClick="notImplemented()">\
</td></tr></table></form>';
    } else {
        document.getElementById('impkeys').innerHTML = '';
        
        if (client > 1) {
        
        document.getElementById('impkey').innerHTML = '<form method=post action=import_keys.php id=add_key name=add_key>\
<input name=act value=impkey type=hidden>\
<table border=0 width=100%>\
<tr><td>Client-ID:</td>\
<td align=left><input name=client readonly id=client size=8 maxlength=20 value=' + client + '>\
</td></tr>\
<tr><td>Yubikey tokenID (in base64):</td>\
<td align=left><input name=otp id=otp size=90 maxlength=99>\
</td></tr>\
<tr><td>AES Secret:</td>\
<td align=left><input name=sec id=sec size=90 maxlength=200>\
</td></tr>\
<tr><td>Life Cycle State:</td>\
<td align=left><select name=active>\
<option value="1">Active\
<option value="0">Inactive\
</select>\
</td></tr>\
<tr><td>Note about this Yubikey:</td>\
<td align=left><input name=notes id=notes size=60 maxlength=99>\
<p><input type=submit class=buttonLinkO value="Add It">\
</td></tr></table></form>';
		} else {
		        document.getElementById('impkey').innerHTML = '<form method=post action=import_keys.php id=add_key name=add_key>\
<input name=act value=impkey type=hidden>\
<table border=0 width=100%>\
<tr><td>Client-ID:</td>\
<td align=left><input name=client id=client size=8 maxlength=99 value=' + client + '>\
</td></tr>\
<tr><td>Yubikey TokenID or OTP (in modhex):</td>\
<td align=left><input name=otp id=otp size=90 maxlength=99>\
</td></tr>\
<tr><td>AES Secret:</td>\
<td align=left><input name=sec id=sec size=90 maxlength=200>\
</td></tr>\
<tr><td>Life Cycle State:</td>\
<td align=left><select name=active>\
<option value="1">Active\
<option value="0">Inactive\
</select>\
</td></tr>\
<tr><td>Note about this Yubikey:</td>\
<td align=left><input name=notes id=notes size=60 maxlength=99>\
<p><input type=submit class=buttonLinkO value="Add It">\
</td></tr></table></form>';
		}
    }
}

function ismaxlength(obj){
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
    obj.value=obj.value.substring(0,mlength)
}

function submitAddClient() {
	
	var form = document.add_client;
	
	form.email.value = trimString(form.email.value);
	if (form.email.value.length < 5) {
		alert("Enter the client's email");
		return;
	} else if (! valid_email(form.email.value)) {
		return;
	}

	form.otp.value = trimString(form.otp.value);
	form.pin.value = trimString(form.pin.value);
	
	if (form.otp.value.length > 1 && form.pin.value.length < 4) {
		alert("Missing password");
		return;
	}
	
	form.submit();
}

function submitAddAdminKey() {
	
	var form = document.add_admin_key;
	
	form.client_id.value = trimString(form.client_id.value);
	if (form.client_id.value.length < 1) {
		alert("Enter the client's ID");
		return;
	}

	form.otp.value = trimString(form.otp.value);
	if (form.otp.value.length < 10) {
		alert("Generte an OTP from the client's Yubikey");
		return;
	}

	form.pin.value = trimString(form.pin.value);
	if (form.pin.value.length < 1) {
		alert("Missing password!");
		return;
	}
	
	form.submit();
}

function notImplemented() {
	alert('Not yet implemented, coming soon...');
}
